//
//	 ______    ______    ______    
//	/\  __ \  /\  ___\  /\  ___\   
//	\ \  __<  \ \  __\_ \ \  __\_ 
//	 \ \_____\ \ \_____\ \ \_____\ 
//	  \/_____/  \/_____/  \/_____/ 
//
//	Powered by BeeFramework
//
//
//  GuideViewController.m
//  dingding
//
//  Created by LHL on 14/12/18.
//  Copyright (c) 2014年 com.gordon. All rights reserved.
//

#import "GuideViewController.h"
#import "CustomPageControl.h"
#import "AppBoard_iPhone.h"
#pragma mark -

@interface GuideViewController()<UIScrollViewDelegate>
{
	//<#@private var#>
}
@end

@implementation GuideViewController{
    int currentPage;
    CustomPageControl *pageControl;
    UIImageView *guideView;
    UIButton *confirm;

}

- (void)load
{
}

- (void)unload
{
}

#pragma mark - Signal

ON_CREATE_VIEWS( signal )
{
    UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    scroll.showsHorizontalScrollIndicator = NO;
    scroll.showsVerticalScrollIndicator = NO;
    scroll.bounces = NO;
    scroll.delegate = self;
    scroll.pagingEnabled = YES;
    scroll.directionalLockEnabled = YES;
    for(int i=0;i<5;i++)
    {
        guideView = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].applicationFrame.size.width*i,0, [UIScreen mainScreen].applicationFrame.size.width, [UIScreen mainScreen].bounds.size.height)];
        guideView.tag = i+1;
        //if(i==0)
        guideView.image = [UIImage imageNamed:[NSString stringWithFormat:@"guide0%d.png",i+1]];
        guideView.userInteractionEnabled=YES;
        [scroll addSubview:guideView];
        scroll.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width*(i+1), 0);
    }
    [self.view addSubview:scroll];
    pageControl = [[CustomPageControl alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].applicationFrame.size.height-50, 0, 20)];
    pageControl.numberOfPages = 4;
    pageControl.currentPage = 0;
    [self.view addSubview:pageControl];
    confirm = [UIButton buttonWithType:UIButtonTypeCustom];
    confirm.frame = CGRectMake([UIScreen mainScreen].applicationFrame.size.width*0.25, [UIScreen mainScreen].applicationFrame.size.height-78, 159, 32);
    confirm.backgroundImage = [UIImage imageNamed:@"guide_button.png"];
    [confirm setTitle:@"加入叮叮" forState:0];
    [confirm addTarget:self action:@selector(hideGuide) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:confirm];
    confirm.hidden = YES;
    confirm.alpha = 0.0;
    
}

#pragma mark - ScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat pageWidth = self.view.frame.size.width;
    int page =  floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    pageControl.currentPage = page;
//    NSLog(@"%d",page);
    if (page == 4) {
        [UIView animateWithDuration:0.2 animations:^{
            pageControl.alpha = 0.0;
            confirm.enabled = YES;
            confirm.hidden = NO;
            confirm.alpha = 1.0;
        }];
    }else{
        [UIView animateWithDuration:0.2 animations:^{
            pageControl.alpha = 1.0;
            confirm.enabled = NO;
            confirm.alpha = 0.0;
        }completion:^(BOOL finish){
            confirm.hidden = YES;
        }];
    }
}

-(void)show{
    [[AppBoard_iPhone sharedInstance].view addSubview:self.view];
    [self performSelector:@selector(syncGuideStatus) withObject:nil afterDelay:1];
}

-(void)syncGuideStatus{
    [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"guide"];
    [[NSUserDefaults standardUserDefaults]synchronize];
}

-(void)hideGuide{
    [UIView animateWithDuration:0.4 animations:^{
        self.view.alpha = 0.0;
    }completion:^(BOOL finish){
        [[NSNotificationCenter defaultCenter]postNotificationName:@"guide" object:nil];
        [self.view removeFromSuperview];
    }];
}

ON_DELETE_VIEWS( signal )
{
}

ON_LAYOUT_VIEWS( signal )
{
}

ON_WILL_APPEAR( signal )
{
}

ON_DID_APPEAR( signal )
{
}

ON_WILL_DISAPPEAR( signal )
{
}

ON_DID_DISAPPEAR( signal )
{
}

ON_SIGNAL3( BeeUINavigationBar, LEFT_TOUCHED, signal )
{
}

ON_SIGNAL3( BeeUINavigationBar, RIGHT_TOUCHED, signal )
{
}

@end
